package com.itheima;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.Dao.StudentMapper;
import com.itheima.pojo.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class Mybatis2 {
    @Autowired
    private StudentMapper studentMapper;

    //-- 查询 math 分数大于 80 分的学生
    @Test
    public void testGetAll1() {
        LambdaQueryWrapper<Student> qw = new LambdaQueryWrapper<>();
        qw.gt(Student::getMath, 80);
        List<Student> students = studentMapper.selectList(qw);
        for (Student student : students) {
            System.out.println(student);
        }
    }

    //-- 查询 english 分数小于或等于 80 分的学生
    @Test
    public void testGetAll2() {
        LambdaQueryWrapper<Student> qw = new LambdaQueryWrapper<>();
        qw.le(Student::getEnglish, 80);
        List<Student> students = studentMapper.selectList(qw);
        for (Student student : students) {
            System.out.println(student);
        }
    }

    //-- 查询 age 等于 20 岁的学生
    @Test
    public void testGetAll3() {
        LambdaQueryWrapper<Student> qw = new LambdaQueryWrapper<>();
        qw.eq(Student::getAge, 20);
        List<Student> students = studentMapper.selectList(qw);
        for (Student student : students) {
            System.out.println(student);
        }
    }

    //-- 查询 age 不等于 20 岁的学生
    @Test
    public void testGetAll4() {
        LambdaQueryWrapper<Student> qw = new LambdaQueryWrapper<>();
        qw.ne(Student::getAge, 20);
        List<Student> students = studentMapper.selectList(qw);
        for (Student student : students) {
            System.out.println(student);
        }
    }

    //-- 查询 age 大于 35 且性别为男的学生(两个条件同时满足)
    @Test
    public void testGetAll5() {
        LambdaQueryWrapper<Student> qw = new LambdaQueryWrapper<>();
        qw.gt(Student::getAge, 35).
                eq(Student::getSex, "男");
        List<Student> students = studentMapper.selectList(qw);
        for (Student student : students) {
            System.out.println(student);
        }
    }

    //-- 查询 age 大于 35 或性别为男的学生(两个条件其中一个满足)
    @Test
    public void testGetAll6() {
        LambdaQueryWrapper<Student> qw = new LambdaQueryWrapper<>();
        qw.gt(Student::getAge, 35).or().eq(Student::getSex, "男");
        List<Student> students = studentMapper.selectList(qw);
        for (Student student : students) {
            System.out.println(student);
        }
    }

    //-- 查询 id 是 1 或 3 或 5 的学生
    @Test
    public void testGetAll7() {
        LambdaQueryWrapper<Student> qw = new LambdaQueryWrapper<>();
        qw.eq(Student::getId, 1).
                or().
                eq(Student::getId, 3).
                or().
                eq(Student::getId, 5);
        List<Student> students = studentMapper.selectList(qw);
        for (Student student : students) {
            System.out.println(student);
        }
    }

    //-- 查询 english 成绩大于等于 75，且小于等于 90 的学生
    @Test
    public void testGetAll8() {
        LambdaQueryWrapper<Student> qw = new LambdaQueryWrapper<>();
        qw.ge(Student::getEnglish, 75).
                le(Student::getEnglish, 90);
        List<Student> students = studentMapper.selectList(qw);
        for (Student student : students) {
            System.out.println(student);
        }
    }

    //-- 查询姓马的学生
    @Test
    public void testGetAll9() {
        LambdaQueryWrapper<Student> qw = new LambdaQueryWrapper<>();
        qw.likeRight(Student::getName, "马");
        List<Student> students = studentMapper.selectList(qw);
        for (Student student : students) {
            System.out.println(student);
        }
    }

    //-- 查询姓名中包含'德'字的学生
    @Test
    public void testGetAll10() {
        LambdaQueryWrapper<Student> qw = new LambdaQueryWrapper<>();
        qw.like(Student::getName, "德");
        List<Student> students = studentMapper.selectList(qw);
        for (Student student : students) {
            System.out.println(student);
        }
    }
}
